-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
43 lines (34 loc) · 987 Bytes
/
Solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
// Function to find the minimum number of coins
void findMinCoins(int coins[], int n, int amount) {
int count = 0;
printf("Coins used: ");
for (int i = 0; i < n; i++) {
while (amount >= coins[i]) {
amount -= coins[i];
printf("%d ", coins[i]);
count++;
}
}
if (amount > 0) {
printf("\nCannot make the exact amount with the given denominations.");
} else {
printf("\nMinimum number of coins needed: %d\n", count);
}
}
int main() {
int n, amount;
// Input the number of coin denominations
printf("Enter the number of coin denominations: ");
scanf("%d", &n);
int coins[n];
printf("Enter the coin denominations in descending order: ");
for (int i = 0; i < n; i++) {
scanf("%d", &coins[i]);
}
// Input the amount
printf("Enter the amount: ");
scanf("%d", &amount);
findMinCoins(coins, n, amount);
return 0;
}